<?php
// html.forms.php

// This is a custom class for dynamic html forms


require_once("html.forms.defs.php");

class html_forms {
	
	var $formname;
	var $formtype;
	var $submiturl;
	var $formcaption;
	var $enctype;
	var $reqFields;
	
	var $formerror;
	
	var $formFields = array();
		
	function setFormData($fName, $fType, $fUrl,$fCaption,$fEncType = "application/x-www-form-urlencoded") {
		$this->formname = $fName;
		$this->formtype = $fType;
		$this->submiturl = $fUrl;
		$this->formcaption = $fCaption;
		$this->enctype = $fEncType;
	}
	
	function setValidate($validate_pointer) {
		$this->reqFields = $validate_pointer;	
	}
	
	function setFormError($fErrorMessage) {
		// Set the class variable for the error message field
		$possible_errors['password'] = FORM_ERROR_PASSWORD;
		$possible_errors['missing'] = FORM_ERROR_MISSINGFIELD;
		$possible_errors['nosuchuser'] = FORM_ERROR_BADUSER;
		$possible_errors['passwordmismatch'] = FORM_ERROR_BADLOGIN;
		$possible_errors['badvercode'] = FORM_ERROR_BADVERIFYCODE;
		$possible_errors['userexists'] = FORM_ERROR_USEREXISTS;
		$this->formerror = $possible_errors[$fErrorMessage];	
	}
	
	function setFormFields($fieldDefinition) {
		// Dynamically defines fields and returns the array	
		/* DEBUG INFORMATION 
		echo '<pre>';
		print_r($fieldDefinition);
		echo '</pre>';
		*/
		$this->formFields = $fieldDefinition;
	}
	
	function createField_input($fieldName,$value,$label,$size=20) {
		$field = sprintf('<input type="text" name="%s" value="%s" size="%s" />',$fieldName,$value,$size);
		
		if($this->isRequired($fieldName,$this->reqFields) == TRUE) {
			$field .= " *";
		}
		$field_return = array("label" => $label, "field" => $field);
		return $field_return;
	}
	
	function createField_text($fieldName,$label,$rows,$columns) {
		$field = sprintf('<textarea name="%s" rows="%s" cols="%s"></textarea>',$fieldName,$rows,$columns);

		if($this->isRequired($fieldName,$this->reqFields) == TRUE) {
			$field .= " *";
		}

		$field_return = array("label" => $label, "field" => $field);
		return $field_return;
	}
	
	function createField_password($fieldName,$label,$size=20) {
		$field = sprintf('<input type="password" name="%s" size="%s" />',$fieldName,$size);
		
		if($this->isRequired($fieldName,$this->reqFields) == TRUE) {
			$field .= " *";
		}		
		
		$field_return = array("label" => $label, "field" => $field);
		return $field_return;
	}
	
	function createField_option($fieldName, $label, $options) {
		$field = sprintf('<select name="%s">',$fieldName);
		//print_r($options);
		foreach ($options as $option) {
			$option_value = $option["value"];
			$option_text = $option["text"];
			$option_default = $option["default"];
			$field .= sprintf('<option value="%s" %s>%s</option>',$option_value,$option_default,$option_text);
		}
		$field .= '</select>';
		
		if($this->isRequired($fieldName,$this->reqFields) == TRUE) {
			$field .= " *";
		}		
		
		$field_return = array("label" => $label, "field" => $field);
		return $field_return;
	}
	
	function createField_hidden($fieldName, $value) {
		$field = sprintf('<input type="hidden" name="%s" value="%s" />',$fieldName,$value);
		$field_return = array("label" => FALSE, "field" => $field, "hidden" => TRUE);
		return $field_return;
	}
	
	function createField_button($fieldName, $value, $label, $code="") {
		$field = sprintf('<input type="button" name="%s" value="%s" %s />',$fieldName,$value,$code);
		$field_return = array("label" => $label, "field" => $field);
		return $field_return;
	}
	
	function createField_state_dropdown($fieldName,$label) {
		$field = sprintf('<select name="%s">',$fieldName);
		$field .= '         <option></option> 
                            <option value="AK">AK</option> 
                            <option value="AL">AL</option> 
                            <option value="AR">AR</option> 
                            <option value="AZ">AZ</option> 
                            <option value="CA">CA</option> 
                            <option value="CO">CO</option> 
                            <option value="CT">CT</option> 
                            <option value="DC">DC</option> 
                            <option value="DE">DE</option> 
                            <option value="FL">FL</option> 
                            <option value="GA">GA</option> 
                            <option value="HI">HI</option> 
                            <option value="IA">IA</option> 
                            <option value="ID">ID</option>
                            <option value="IL">IL</option> 
                            <option value="IN">IN</option> 
                            <option value="KS">KS</option> 
                            <option value="KY">KY</option> 
                            <option value="LA">LA</option> 
                            <option value="MA">MA</option> 
                            <option value="MD">MD</option> 
                            <option value="ME">ME</option> 
                            <option value="MI">MI</option> 
                            <option value="MN">MN</option> 
                            <option value="MO">MO</option> 
                            <option value="MS">MS</option> 
                            <option value="MT">MT</option> 
                            <option value="NC">NC</option> 
                            <option value="ND">ND</option> 
                            <option value="NE">NE</option> 
                            <option value="NH">NH</option> 
                            <option value="NJ">NJ</option> 
                            <option value="NM">NM</option> 
                            <option value="NV">NV</option> 
                            <option value="NY">NY</option> 
                            <option value="OH">OH</option> 
                            <option value="OK">OK</option> 
                            <option value="OR">OR</option> 
                            <option value="PA">PA</option> 
                            <option value="RI">RI</option> 
                            <option value="SC">SC</option> 
                            <option value="SD">SD</option> 
                            <option value="TN">TN</option> 
                            <option value="TX">TX</option> 
                            <option value="UT">UT</option> 
                            <option value="VA">VA</option> 
                            <option value="VT">VT</option>
                            <option value="WA">WA</option> 
                            <option value="WI">WI</option> 
                            <option value="WV">WV</option> 
                            <option value="WY">WY</option> 
                            <option value="AA">AA</option> 
                            <option value="AE">AE</option> 
                            <option value="AP">AP</option> 
                            <option value="AS">AS</option> 
                            <option value="FM">FM</option> 
                            <option value="GU">GU</option> 
                            <option value="MH">MH</option> 
                            <option value="MP">MP</option> 
                            <option value="PR">PR</option> 
                            <option value="PW">PW</option> 
                            <option value="VI">VI</option>';
		$field .= '</select>';
		$field_return = array("label" => $label, "field" => $field);
		return $field_return;
	}
	
	function createField_submit($fieldName, $value) {
		$field = sprintf('<input type="submit" name="%s" value="%s" />',$fieldName,$value);
		$field_return = array("label" => FALSE, "field" => $field);
		return $field_return;
	}
	
	function createField_oneline_multifield($label, $fieldArray) {
		$field = '<tr><td width="35%" class="form_label_cell">' . $label . '</td><td width="65%">';
		foreach ($fieldArray as $fieldMerge) {
			$field .= $fieldMerge["field"] . '&nbsp;&nbsp;';
		}
		$field .= '</td></tr>';
		$field_return = array("label" => "customRow", "field" => $field);
		return $field_return;
		//print_r($field_return);
	}
	
	function createFormSection($sectionLabel) {
		$field = '<tr><td colspan="2" class="form_section_label"><p>' . $sectionLabel . '</p></td></tr>';
		$field_return = array("label" => "customRow", "field" => $field);
		return $field_return;
	}
	
	function createFormSectionLink($linkArray) {
		$customLinks = array();
		
		foreach ($linkArray as $link) {
			$linkCode = sprintf('<a href="%s">%s</a>',$link["link"],$link["text"]);
			array_push($customLinks,$linkCode);
		}
		$linkHTML = implode(" | ", $customLinks);
		$field = '<tr><td colspan="2" align="center">' . $linkHTML . '</td></tr>';
		$field_return = array("label" => "customRow", "field" => $field);
		
		return $field_return;
	}
	
	function createFormSectionText($sectionText,$align="center",$colspan = TRUE,$textCol = 1) {
		$cellHolder = array("&nbsp;","&nbsp;");
		if ($colspan == TRUE) {
			$textCode = sprintf('<tr><td colspan="2" align="%s">%s</td></tr>',$align,$sectionText);	
		} else {
			$cellHolder[$textCol];
			$textCode = '<tr><td width="35%" align="' . $align . '">' . $cellHolder[0] . '</td><td width="65%" align="' . $align . '">';
			$textCode .= $sectionText . '</td></tr>';
		}
		
		$field_return = array("label" => "customRow", "field" => $textCode);
		return $field_return;
	}
	
	function createFieldDisplayText($text) {
		$field_return = array("label" => "customRow", "field" => $text);
		return $field_return;
	}
	
	function isRequired($fieldName,$requiredArray) {
		$is_required = FALSE;
		
		foreach ($requiredArray as $required) {
			if ($fieldName == $required) {
					$is_required = TRUE;
			}
		}
		
		return $is_required;
	}
	
	function draw_form($formTable_width = "100%") {
		$form_html = sprintf('<form name="%s" method="%s" action="%s" enctype="%s">', $this->formname,$this->formtype,$this->submiturl,$this->enctype);
		$form_html .= '<div align="center">
<table width="' . $formTable_width . '" border="0" cellpadding="0">
	<tr><td class="form_caption_cell" colspan="2"><p>' . $this->formcaption . '</p></td></tr><tr><td colspan="2" class="form_error">' . $this->formerror . '</td></tr>';
		foreach ($this->formFields as $formField) {
			if ($formField["label"] != "customRow") {
				if ($formField["label"] != FALSE) {
					$form_html .= '
	<tr><td class="form_label_cell" width="35%">' . $formField["label"] . '</td><td width="65%">' . $formField["field"] . '</td></tr>';
				} else {
					if ($formField["hidden"] != TRUE) {
						$form_html .= '
	<tr><td class="form_label_cell" colspan="2">' . $formField["field"] . '</td></tr>';
					} else {
						$form_html .= $formField["field"];	
					}
				}
			} else {
				$form_html .= '<!--custom row-->
	' . $formField["field"];	
			}
		}
		$form_html .= '</table></div></form>';
		return $form_html;
	}
}
?>